package com.kingcore.director.core.ui;

import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import wzw.util.ComponentUtil;

import com.sun.awt.AWTUtilities;

public class ProcessingDialog extends Thread {
	private Thread currentThread = null;// 实际调用时就是TestThread事务处理线程
	private String messages = "";// 提示框的提示信息
	private static Container parent = null;// 提示框的父窗体
	private static JDialog clueDiag = null;// “线程正在运行”提示框
	private Dimension dimensions = Toolkit.getDefaultToolkit().getScreenSize();
	private int width = 35;//dimensions.width / 4, 
	private int height = 41;
	private int left = 0, top = 0;

	public static JDialog showInstance(Dialog parent, Thread currentThread,
			String messages) {
		clueDiag = new JDialog(parent, "正在执行，请等待...", true);
		ProcessingDialog.parent = parent;
		(new ProcessingDialog( currentThread, messages)).start();
		return clueDiag; 
	}

	public static JDialog showInstance(Frame parent, Thread currentThread,
			String messages) {
		clueDiag = new JDialog(parent, "正在执行，请等待...", true);
		(new ProcessingDialog( currentThread, messages)).start();
		ProcessingDialog.parent = parent;
		return clueDiag;
	}
	public ProcessingDialog( Thread currentThread, String messages) {
		this.currentThread = currentThread;
		this.messages = messages;
		initDiag();// 初始化提示框
		currentThread.start();
	}

	protected void initDiag() {
		//clueDiag = new JDialog(parent, "正在执行，请等待...", true);
		clueDiag.setCursor(new Cursor(Cursor.WAIT_CURSOR));
		clueDiag.setSize(38, 38);
		final ImageIcon i = new ImageIcon(
				"./resource/processing.gif");  
		JPanel testPanel = new JPanel();
		JLabel testLabel = new JLabel(i);
		clueDiag.getContentPane().add(testPanel);
		testPanel.add(testLabel);
		(new DisposeDiag()).start();// 启动关闭提示框线程
	}
	
	public void run() {
		// 显示提示框
		int left = (dimensions.width - width) / 2;
		int top = (dimensions.height - height) / 2;
		clueDiag.setUndecorated(true);
		clueDiag.setSize(new Dimension(width, height));
		AWTUtilities.setWindowOpacity(clueDiag, 0.8f); //设置透明度
		clueDiag.setLocation(left, top);
		//System.out.println("===========1");
//		DirectorComponentUtils.move2Centre(obj)
		ComponentUtil.setLocationToCenter(ProcessingDialog.parent, clueDiag);
		clueDiag.setVisible(true);
		//System.out.println("===========2");
	}

	class DisposeDiag extends Thread {
		public void run() {
			try {
				currentThread.join();// 等待事务处理线程结束
			} catch (InterruptedException e) {
				System.out.println("Exception:" + e);
			}
			clueDiag.dispose();// 关闭提示框
		}
	}

}
